<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2013-2020 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w <y139w@163.com>
 * +--------------------------------------------------------------
 * | 2020/2/5 22:33 Yier
 * +--------------------------------------------------------------
 * | Uploading.php
 * +--------------------------------------------------------------
 */

namespace luo;

use luo\upload\Upload;

class Uploading
{

    private $rootPath = '';

    private $savePath = '';

    private $attached = 'uploads/';

    private $size = 2;

    private $extArray = [
        'image' => ['gif', 'jpg', 'jpeg', 'png', 'bmp'],
        'flash' => ['swf', 'flv'],
        'media' => ['swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'],
        'file'  => ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'],
    ];

    private $allowExt = ['jpg', 'jpeg', 'png', 'bmp'];

    private $_files = null;

    private $files = null;

    public function __construct($attached = '')
    {
        $scriptFilenameDirectory = dirname($_SERVER['SCRIPT_FILENAME']);
        $this->rootPath = $scriptFilenameDirectory . '/';
        $root = str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFilenameDirectory);
        $this->savePath = ($root === '/') ? '/' : $root . '/';
        $this->attached = empty($attached) ? $this->attached : $attached;
        $this->_files = $_FILES;
        $filesArray = [];
        foreach($this->_files as $key=>$val){
            $val['key'] = $key;
            $upload = new Upload($val, $this->attached);
            $nameArray = explode('.', $val['name']);
            $ext = $nameArray[count($nameArray) - 1];
            $filesArray[$key] = $upload
                ->setRootPath($this->rootPath)
                ->setSavePath($this->savePath)
                ->setAttached($this->attached)
                ->setSizeLimit($this->size * 1024 * 1024)
                ->setExtDirectory($this->extDirectoryHandle($ext))
                ->setAllowExt(in_array($ext, $this->allowExt));
        }
        if(count($filesArray) > 0){
            $this->files = $filesArray;
        }
    }

    /**
     * @param string $file
     * @return mixed
     */
    public function file($file = ''){

        $filesArray = array_values($this->files);
        $fileObject = $filesArray[0];
        if(!empty($file) && !empty($this->files[$file])){
            $fileObject = $this->files[$file];
        }
        return $fileObject;

    }

    /**
     * @param $ext
     * @return int|string
     */
    private function extDirectoryHandle($ext){

        $directoryName = 'image';
        foreach($this->extArray as $directory=>$directoryExtArray){
            if(in_array($ext, $directoryExtArray)){
                $directoryName = $directory;
                break;
            }
        }
        return $directoryName;

    }

}